package com.gitee.simpleplus.utils;

import java.io.InputStreamReader;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONReader;

public class JsonUtils {
	private static final Logger log = LoggerFactory.getLogger(JsonUtils.class);
	
	private static final String charset = "UTF-8";
	
	private JsonUtils(){}
	
	public static <T> T parseObjFromFile(String filePath, Class<T> clazz){
		try(JSONReader r = new JSONReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream(filePath), charset))){
			return JSON.parseObject(r.readString(), clazz);
		}catch (Exception e) {
			log.error("parseObjFromFile->", e);
			throw new RuntimeException(e);
		}
	}

	public static <T> List<T> parseArrayFromFile(String filePath, Class<T> clazz){
		try(JSONReader r = new JSONReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream(filePath), charset))){
			return JSON.parseArray(r.readString(), clazz);
		}catch (Exception e) {
			log.error("parseArrayFromFile->", e);
			throw new RuntimeException(e);
		}
	}
}
